package tree.easy;

import tree.TreeNode;

/**
 * 226. 翻转二叉树
 */
public class invertTree {

//    public TreeNode invertTree(TreeNode root) {
//        TreeNode treeNode = null;
//        if (root != null) {
//            treeNode = new TreeNode(root.val);
//            help(root, treeNode);
//        }
//        return treeNode;
//    }

    public void help(TreeNode root, TreeNode afterRoot) {
        if (root != null) {
            if (root.left != null) {
                afterRoot.right = new TreeNode(root.left.val);
                help(root.left, afterRoot.right);
            }
            if (root.right != null) {
                afterRoot.left = new TreeNode(root.right.val);
                help(root.right, afterRoot.left);
            }
        }
    }

    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }

        TreeNode temp = invertTree(root.left);
        root.left = invertTree(root.right);
        root.right = temp;
        return root;
    }

}
